<?php
namespace Admin\Controller;
use Think\Controller;
class WxController extends BaseController{
	public static $wx;//缓存微信对象
	protected function _initialize(){
		parent::_initialize();
		
		$options['appid']=C('BB_APPID_client');
		$options['secret']=C('BB_APPID_client');
		self::$wx=new \bb\Wechat($options);		//new 微信类
	}
    public function menu(){
		//设置面包导航，主加载器请配置
		$bread=array(
			'0'=>array(
				'name'=>'微信',
				'url'=>U('Admin/Wx/index')
			),
			'1'=>array(
				'name'=>'查看自定义菜单',
				'url'=>U('Admin/Wx/menu')
			)
		);
		
		
		$this->assign('breadhtml',$this->getBread($bread));
		$m=M('weixin_nemu');
		$wxnemu1=$m->where(array('wx_id'=>1))->order('sort desc,id desc')->select();
		$wxnemu2=$m->where(array('wx_id'=>2))->order('sort desc,id desc')->select();
		$wxnemu3=$m->where(array('wx_id'=>3))->order('sort desc,id desc')->select();
		
		$cachewxnemu1=\bb\Data::tree($wxnemu1, "name", "id", "pid");
		$cachewxnemu2=\bb\Data::tree($wxnemu2, "name", "id", "pid");
		$cachewxnemu3=\bb\Data::tree($wxnemu3, "name", "id", "pid");
		$this->assign('wxnemu1',$cachewxnemu1);
		$this->assign('wxnemu2',$cachewxnemu2);
		$this->assign('wxnemu3',$cachewxnemu3);
		$this->display();
    }
	//获取微信自定义菜单
	public function getmenu(){
		$menudata=self::$wx->menus();
		if($menudata){
			$info['status']=1;
			$info['msg']='获取成功！';
			$info['data']=$menudata;
		}else{
			$info['status']=0;
			$info['msg']='获取失败！';
		}
		$this->ajaxreturn($info);
	}
	//设置微信自定义菜单
	public function menu_set(){
		//设置面包导航，主加载器请配置
		$bread=array(
			'0'=>array(
				'name'=>'微信',
				'url'=>U('Admin/Wx/index')
			),
			'1'=>array(
				'name'=>'查看自定义菜单',
				'url'=>U('Admin/Wx/menu')
			),
			'2'=>array(
				'name'=>'设置自定义菜单',
				'url'=>U('Admin/Wx/menu_set')
			)
		);
		$this->assign('breadhtml',$this->getBread($bread));
		//代码部分
		$id=I('id');
		$wx_id=I('wx_id');
		if(!$wx_id){
			echo '出错了！';
			echo '<a id="alink" href="'.U('Wx/menu').'" data-loader="bb-loader" data-loadername=""></a>'; 
			echo "<script>initLoader('bb-loader'); setTimeout(function(){ document.getElementById('alink').click(); },1000); </script>";
			exit;
		}
		$m=M('weixin_nemu');
		if(IS_POST){
			$p=I('post.');
			if($id){
				$re=$m->save($p);
				if($re){
					$info['status']=1;
					$info['msg']='修改成功！';
				}else{
					$info['status']=0;
					$info['msg']='修改失败！';
				}
				$this->ajaxReturn($info);
			}else{
				if($p['pid']){
					$num=$m->where(array('pid'=>$p['pid']))->count();
					if($num != 5){
						$re=$m->add($p);
						if($re){
							$info['status']=1;
							$info['msg']='添加成功！';
						}else{
							$info['status']=0;
							$info['msg']='添加失败！';
						}
						$this->ajaxReturn($info);
					}else{
						$info['status']=0;
						$info['msg']='微信中二级菜单做多可以有5个！';
						$this->ajaxReturn($info);
					}
				}else{
					$num=$m->where(array('pid'=>0,'wx_id'=>$wx_id))->count();
					if($num != 3){
						$re=$m->add($p);
						if($re){
							$info['status']=1;
							$info['msg']='添加成功！';
						}else{
							$info['status']=0;
							$info['msg']='添加失败！';
						}
						$this->ajaxReturn($info);
					}else{
						$info['status']=0;
						$info['msg']='微信中一级菜单做多可以有3个！';
						$this->ajaxReturn($info);
					}
				}
				
			}
			$this->ajaxReturn($info);
		}
		$nemupid=$m->where(array('pid'=>0,'wx_id'=>$wx_id))->select();
		$this->assign('nemupid',$nemupid);
		$nemudata=$m->where(array('id'=>$id))->find();
		$this->assign('nemudata',$nemudata);
		$this->assign('wx_id',$wx_id);
		$this->display();
    }
	//更新微信自定义菜单
	public function menu_up(){
		if(IS_POST){
			$p=I('post.');
			$mp['wx_id']=$wx_id=$p['wxtype'];
			$m=M('weixin_nemu');
			$nemudata=$m->where($mp)->order('sort desc,id desc')->select();
			$nemudata=\bb\Data::tree($nemudata, "name", "id", "pid");
			$data=array();
			foreach ($nemudata as $k => $v) {if($v['pid']==0){$data[]=$v;}}
			foreach ($data as $k => $v) {
				foreach ($nemudata as $sk => $sv){
					if($v['id']==$sv['pid']){
						unset($sv['media_id']);
						unset($sv['sort']);
						unset($sv['id']);
						unset($sv['pid']);
						unset($sv['_level']);
						unset($sv['_html']);
						unset($sv['_first']);
						unset($sv['_end']);
						unset($sv['_name']);
						if($sv['type']=='click'){
							unset($sv['url']);
						}else{
							unset($sv['key']);
						}
						$sv['name']=htmlspecialchars_decode($sv['name']);
						$sv['url']=htmlspecialchars_decode($sv['url']);
						$data[$k]['sub_button'][]=$sv;
					}
				}
				unset($data[$k]['media_id']);
				unset($data[$k]['sort']);
				unset($data[$k]['id']);
				unset($data[$k]['pid']);
				unset($data[$k]['_level']);
				unset($data[$k]['_html']);
				unset($data[$k]['_name']);
				if($data[$k]['type']=='click'){
					unset($data[$k]['url']);
				}else{
					unset($data[$k]['key']);
				}
				$data[$k]['name']=htmlspecialchars_decode($v['name']);
				$data[$k]['url']=htmlspecialchars_decode($v['url']);
				
				if(!$data[$k]['url']){
					unset($data[$k]['url']);
				}
				if(!$data[$k]['key']){
					unset($data[$k]['key']);
				}
			}
			$buttondata=array('button'=>$data);
			
			if($wx_id==1){
				foreach ($buttondata['button'] as $k => $v) {
					unset($buttondata['button'][$k]['wx_id']);
				}
				$options['appid']=C('BB_APPID_client');
				$options['secret']=C('BB_SECRET_client');
				self::$wx=new \bb\Wechat($options);		//new 微信类
				
				$re=self::$wx->menu_create($buttondata);
				if($re){
					$info['status']=1;
					$info['msg']='更新成功！';
				}else{
					$info['status']=0;
					$info['msg']='更新失败！';
				}
				$this->ajaxreturn($info);
			}
			
			if($wx_id==2){
				foreach ($buttondata['button'] as $k => $v) {
					unset($buttondata['button'][$k]['wx_id']);
				}
				$options['appid']=C('BB_APPID_business');
				$options['secret']=C('BB_SECRET_business');
				self::$wx=new \bb\Wechat($options);		//new 微信类
				$re=self::$wx->menu_create($buttondata);
				if($re){
					$info['status']=1;
					$info['msg']='更新成功！';
				}else{
					$info['status']=0;
					$info['msg']='更新失败！';
				}
				$this->ajaxreturn($info);
			}
			
			if($wx_id==3){
				foreach ($buttondata['button'] as $k => $v) {
					unset($buttondata['button'][$k]['wx_id']);
				}
				
				$options['appid']=C('BB_APPID_auntie');
				$options['secret']=C('BB_SECRET_auntie');
				self::$wx=new \bb\Wechat($options);		//new 微信类
				$re=self::$wx->menu_create($buttondata);
				
				//p($re);
				if($re){
					$info['status']=1;
					$info['msg']='更新成功！';
				}else{
					$info['status']=0;
					$info['msg']='更新失败！';
				}
				$this->ajaxreturn($info);
			}
			
			$this->ajaxreturn($info);
		}
    }
	//删除微信自定义菜单
	public function menu_del(){
		if(IS_POST){
			$p=I('post.');
			$wx_id=$p['wxtype'];
			
			if($wx_id==1){
				$options['appid']=C('BB_APPID_client');
				$options['secret']=C('BB_SECRET_client');
				self::$wx=new \bb\Wechat($options);		//new 微信类
				$re=self::$wx->menu_delete();
				if($re){
					$info['status']=1;
					$info['msg']='删除成功！';
				}else{
					$info['status']=0;
					$info['msg']='删除失败！';
				}
			}
			
			if($wx_id==2){
				$options['appid']=C('BB_APPID_business');
				$options['secret']=C('BB_SECRET_business');
				self::$wx=new \bb\Wechat($options);		//new 微信类
				$re=self::$wx->menu_delete();
				if($re){
					$info['status']=1;
					$info['msg']='删除成功！';
				}else{
					$info['status']=0;
					$info['msg']='删除失败！';
				}
			}
			
			if($wx_id==3){
				$options['appid']=C('BB_APPID_auntie');
				$options['secret']=C('BB_SECRET_auntie');
				self::$wx=new \bb\Wechat($options);		//new 微信类
				$re=self::$wx->menu_delete();
				if($re){
					$info['status']=1;
					$info['msg']='删除成功！';
				}else{
					$info['status']=0;
					$info['msg']='删除失败！';
				}
			}
			
			$this->ajaxreturn($info);
		}
    }
	//删除数据库菜单
	public function delnemu(){
		$m=M('weixin_nemu');
		$id=I('id');
		if($id){
			$rule=$m->where(array('pid'=>$id))->find();
			if($rule){
				$re=$m->where(array('pid'=>$id))->delete();
				$re=$m->delete($id);
			}else{
				$re=$m->delete($id);
			}
			if($re){
				$info['status']=1;
				$info['msg']='删除成功！';
			}else{
				$info['status']=0;
				$info['msg']='删除失败！';
			}
		}else{
			$info['status']=0;
			$info['msg']='删除失败！';
		}
		$this->ajaxreturn($info);
	}
//================================================================
	//关键词微信回复
	public function keyword(){
		//设置面包导航，主加载器请配置
		$bread=array(
			'0'=>array(
				'name'=>'微信',
				'url'=>U('Admin/Wx/index')
			),
			'1'=>array(
				'name'=>'查看关键词回复',
				'url'=>U('Admin/Wx/keyword')
			)
		);
		$this->assign('breadhtml',$this->getBread($bread));
		//========================================
		$p=$_GET['p']?$_GET['p']:1;
		$psize=C('BB_PSIZE')?C('BB_PSIZE'):20;
		$m=M('weixin_keyword');
		$map['wx_id']=1;
		$map['keyword']=array('neq','首次关注');
		$keyword1=$m->where($map)->page($p,$psize)->select();
		$keywordcount1=$m->where($map)->count();
		$this->getPage($keywordcount1, $psize, 'bb-loader', '关键词回复','bb-search','','page1');
		$this->assign('keyword1',$keyword1);
		$map['wx_id']=2;
		$map['keyword']=array('neq','首次关注');
		$keyword2=$m->where($map)->page($p,$psize)->select();
		$keywordcount2=$m->where($map)->count();
		$this->getPage($keywordcount2, $psize, 'bb-loader', '关键词回复','bb-search','','page2');
		$this->assign('keyword2',$keyword2);
		$map['wx_id']=3;
		$map['keyword']=array('neq','首次关注');
		$keyword3=$m->where($map)->page($p,$psize)->select();
		$keywordcount3=$m->where($map)->count();
		$this->getPage($keywordcount3, $psize, 'bb-loader', '关键词回复','bb-search','','page3');
		$this->assign('keyword3',$keyword3);
		
		$this->display();
	}
	
	//关键词微信回复
	public function keyword_set(){
		//设置面包导航，主加载器请配置
		$bread=array(
			'0'=>array(
				'name'=>'微信',
				'url'=>U('Admin/Wx/index')
			),
			'1'=>array(
				'name'=>'查看关键词回复',
				'url'=>U('Admin/Wx/keyword')
			),
			'2'=>array(
				'name'=>'设置关键词回复',
				'url'=>U('Admin/Wx/menu_set')
			)
		);
		$this->assign('breadhtml',$this->getBread($bread));
		$m=M('weixin_keyword');
		$news=M('weixin_news');
		$id=I('id');
		$wx_id=I('wx_id');
		if(!$wx_id){
			echo '出错了！';
			echo '<a id="alink" href="'.U('Wx/keyword').'" data-loader="bb-loader" data-loadername=""></a>'; 
			echo "<script>initLoader('bb-loader'); setTimeout(function(){ document.getElementById('alink').click(); },1000); </script>";
			exit;
		}
		if(IS_POST){
			$p=I('post.');
			if($id){
				if($p['type']=='text'){
					$add['id']=$p['id'];
					$add['wx_id']=$p['wx_id'];
					$add['keyword']=$p['keyword'];
					$add['type']=$p['type'];
					$add['content']=$p['content'];
					$re=$m->save($add);
					if($re){
						$info['status']=1;
						$info['msg']='修改成功！';
					}else{
						$info['status']=0;
						$info['msg']='修改失败！';
					}
				}else{
					$add['id']=$p['id'];
					$add['wx_id']=$p['wx_id'];
					$add['keyword']=$p['keyword'];
					$add['type']=$p['type'];
					$add['content']=time();
					$re=$m->save($add);
					if($re){
						$datas=array();
						foreach ($p['data'] as $key => $value){
							foreach ($value as $k => $v) {
								$datas[$k][$key]=$v;
							}
						}
						foreach ($datas as $k => $v) {
							$news->save($v);
						}
						$add['content']='';
						$m->save($add);
						$info['status']=1;
						$info['msg']='修改成功！';
					}else{
						$info['status']=0;
						$info['msg']='修改失败！';
					}
				}
			}else{
				$cache=$m->where(array("keyword"=>$p['keyword'],'wx_id'=>$p['wx_id']))->find();
				if($cache){
					$info['status']=0;
					$info['msg']='触发关键词已存在！请更换！';
					$this->ajaxreturn($info);
				}
				if($p['type']=='text'){
					$add['wx_id']=$p['wx_id'];
					$add['keyword']=$p['keyword'];
					$add['type']=$p['type'];
					$add['content']=$p['content'];
					$re=$m->add($add);
					if($re){
						$info['status']=1;
						$info['msg']='添加成功！';
					}else{
						$info['status']=0;
						$info['msg']='添加失败！';
					}
				}else{
					$add['wx_id']=$p['wx_id'];
					$add['keyword']=$p['keyword'];
					$add['type']=$p['type'];
					$add['content']=$p['content'];
					$re=$m->add($add);
					if($re){
						$datas=array();
						foreach ($p['data'] as $key => $value){
							foreach ($value as $k => $v) {
								$datas[$k]['kid']=$re;
								$datas[$k][$key]=$v;
							}
						}
						foreach ($datas as $k => $v) {
							$news->add($v);
						}
						$info['status']=1;
						$info['msg']='添加成功！';
					}else{
						$info['status']=0;
						$info['msg']='添加失败！';
					}
				}
			}
			$this->ajaxreturn($info);
		}
		if($id){
			$cache=$m->where('id='.$id)->find();
			$this->assign('cache',$cache);
			$tmp_data=$news->where('kid='.$id)->select();
			$this->assign('tmp_data',$tmp_data);	
		}
		$this->assign('wx_id',$wx_id);
		$this->display();
	}
	
	//删除关键词微信回复
	public function keyword_del(){
		$id=I('id');//必须使用get方法
		$m=M('weixin_keyword');
		if(!$id){
			$info['status']=0;
			$info['msg']='ID不能为空!';
			$this->ajaxReturn($info);
		}
		$re=$m->delete($id);
		if($re){
			$info['status']=1;
			$info['msg']='删除成功!';
		}else{
			$info['status']=0;
			$info['msg']='删除失败!';
		}
		$this->ajaxReturn($info);
	}
	
//==用户=============================================================================================
	public function user(){
		$bread=array(
			'0'=>array(
				'name'=>'微信',
				'url'=>U('Admin/Wx/index')
			),
			'1'=>array(
				'name'=>'用户管理',
				'url'=>U('Admin/Wx/group')
			)
		);
		$this->assign('breadhtml',$this->getBread($bread));

		$p=$_GET['p']?$_GET['p']:1;
		$psize=C('BB_PSIZE')?C('BB_PSIZE'):20;
		
		$usertype=I('usertype');
		/* ---********************--- */
		$m=M('weixin_client_user');
		//昵称
		if($usertype=='1'){
			if(I('nickname')){
				$search=I('nickname');
				$mp['nickname']=array('like',"%$search%");
				$mp['openid'] = array('like',"%$search%");
				$mp['_logic'] = 'OR';
			}
		}
		$userclient=$m->where($mp)->page($p,$psize)->order('subscribe_time desc')->select();
		$count=$m->where($mp)->count();
		$this->getPage($count, $psize, 'bb-loader', '公众号会员','bb-search','','clientpage');
		$this->assign('userclient',$userclient);
		/* ---********************--- */
		//=========================
		/* ---********************--- */
		$m=M('weixin_business_user');
		//昵称
		if($usertype=='2'){
			if(I('nickname')){
				$search=I('nickname');
				$mp['nickname']=array('like',"%$search%");
				$mp['openid'] = array('like',"%$search%");
				$mp['_logic'] = 'OR';
			}
		}
		$userbusiness=$m->where($mp)->page($p,$psize)->order('subscribe_time desc')->select();
		$businesscount=$m->where($mp)->count();
		$this->getPage($businesscount, $psize, 'bb-loader', '公众号会员','bb-search','','businesspage');
		$this->assign('userbusiness',$userbusiness);
		/* ---********************--- */
		//===========================================
		/* ---********************--- */
		$m=M('weixin_auntie_user');
		//昵称
		if($usertype=='3'){
			if(I('nickname')){
				$search=I('nickname');
				$mp['nickname']=array('like',"%$search%");
				$mp['openid'] = array('like',"%$search%");
				$mp['_logic'] = 'OR';
			}
		}
		$userauntie=$m->where($mp)->page($p,$psize)->order('subscribe_time desc')->select();

		$usercount=$m->where($mp)->count();
		$this->getPage($usercount, $psize, 'bb-loader', '公众号会员','bb-search','',"auntiepage");
		$this->assign('userauntie',$userauntie);
		/* ---********************--- */
		$this->assign('usertype',$usertype);
		$this->display();
	}
//===============================================
	//设置会员备注
	public function remark_set(){
		if(IS_POST){
			$ids=I('ids');
			$usertype=I('usertype');
			if($ids && $usertype){
				$this->assign('ids',$ids);
				//p($usertype);
				$this->assign('usertype',$usertype);
				$this->display();
			}else{
				$p=I('post.');
				$map['id']  = $p['id'];
				$usertype  = $p['usertype'];
				
				if($usertype=='1'){
					$user=M('weixin_client_user')->where($map)->Field('openid,id')->find();
					$options['appid']=C('BB_APPID_client');
					$options['secret']=C('BB_SECRET_client');
					self::$wx=new \bb\Wechat($options);		//new 微信类
					$re=self::$wx->set_user_remark($user['openid'],$p['remark']);
					if($re){
						if($user){
							$userdata=self::$wx->user($user['openid']);
							$userdata['id']=$user['id'];
							M('weixin_client_user')->save($userdata);
						}
						$info['status']=1;
						$info['msg']='设置备注成功！';
					}else{
						$info['status']=0;
						$info['msg']='设置备注失败！';
					}
				}
				
				if($usertype=='2'){
					$user=M('weixin_business_user')->where($map)->Field('openid,id')->find();
					$options['appid']=C('BB_APPID_business');
					$options['secret']=C('BB_SECRET_business');
					self::$wx=new \bb\Wechat($options);		//new 微信类
					$re=self::$wx->set_user_remark($user['openid'],$p['remark']);
					if($re){
						if($user){
							$userdata=self::$wx->user($user['openid']);
							$userdata['id']=$user['id'];
							M('weixin_business_user')->save($userdata);
						}
						$info['status']=1;
						$info['msg']='设置备注成功！';
					}else{
						$info['status']=0;
						$info['msg']='设置备注失败！';
					}
				}
				
				if($usertype=='3'){
					$user=M('weixin_auntie_user')->where($map)->Field('openid,id')->find();
					$options['appid']=C('BB_APPID_auntie');
					$options['secret']=C('BB_SECRET_auntie');
					self::$wx=new \bb\Wechat($options);		//new 微信类
					$re=self::$wx->set_user_remark($user['openid'],$p['remark']);
					if($re){
						if($user){
							$userdata=self::$wx->user($user['openid']);
							$userdata['id']=$user['id'];
							M('weixin_auntie_user')->save($userdata);
						}
						$info['status']=1;
						$info['msg']='设置备注成功！';
					}else{
						$info['status']=0;
						$info['msg']='设置备注失败！';
					}
				}
				
				$info['msg']=$info['msg'].$usertype;
				$this->ajaxReturn($info);
			}
		}else{
			echo '请使用post访问！';
		}
	}
//==============================================
//	//移动会员分组
//	public function mobilegroup(){
//		if(IS_POST){
//			$ids=I('ids');
//			if($ids){
//				$this->assign('ids',$ids);
//				$data=$this->get_group();
//				$this->assign('cache',$data);
//				$this->display();
//			}else{
//				$p=I('post.');
//				$map['id']  = array('in',rtrim($p['id'],","));
//				$openid=M('weixin_user')->where($map)->Field('openid')->select();
//				$cg=0;$sb=0;
//				foreach ($openid as $k => $v) {
//					$re=self::$wx->user_to_group($v['openid'],$p['groupid']);
//					if($re){
//						$map['openid']=$v['openid'];
//						M('weixin_user')->where($map)->save(array('groupid'=>$p['groupid']));
//						$cg=$cg+1;
//					}else{
//						$sb=$sb+1;
//					}
//				}
//
//				$info['status']=1;
//				$info['msg']='成功移动分组'.$cg.'条，移动失败分组'.$sb.'条';
//				
//				$this->ajaxReturn($info);
//			}
//		}else{
//			echo '请使用post访问！';
//		}
//	}
//==分组===============================================================================================	
//	public function get_group(){
//		$authname = 'wechat_group';
//		if($rs = S($authname)){
//			return $rs;
//		}else{
//			$data=self::$wx->groups();
//			S($authname,$data,60*60*2);		//2小时缓存一次
//			return $data;
//		}
//	}
//
//	public function group(){
//		//设置面包导航，主加载器请配置
//		$bread=array(
//			'0'=>array(
//				'name'=>'微信',
//				'url'=>U('Admin/Wx/index')
//			),
//			'1'=>array(
//				'name'=>'查看分组',
//				'url'=>U('Admin/Wx/group')
//			)
//		);
//		$this->assign('breadhtml',$this->getBread($bread));
//		$data=$this->get_group();
//		$this->assign('cache',$data);
//		$this->display();
//	}
//	public function group_set(){
//		//设置面包导航，主加载器请配置
//		$bread=array(
//			'0'=>array(
//				'name'=>'微信',
//				'url'=>U('Admin/Wx/index')
//			),
//			'1'=>array(
//				'name'=>'查看分组',
//				'url'=>U('Admin/Wx/group')
//			),
//			'2'=>array(
//				'name'=>'设置分组',
//				'url'=>U('Admin/Wx/group_set')
//			)
//		);
//		$this->assign('breadhtml',$this->getBread($bread));
//		$id=I('id');
//		if(IS_POST){
//			$p=I('post.');
//			if($id){
//				$name=$p['name'];
//				$re=self::$wx->group_edit($id,$name);
//				//=更新缓存=====
//				$authname = 'wechat_group';
//				S($authname,null);			//删除缓存
//				$data=self::$wx->groups();
//				S($authname,$data,60*60*2);		//2小时缓存一次
//				//=更新缓存=====
//				if($re){
//					$info['status']=1;
//					$info['msg']='修改分组成功!';
//				}else{
//					$info['status']=0;
//					$info['msg']='修改分组失败!';
//				}
//			}else{
//				$name=$p['name'];
//				$re=self::$wx->group_add($name);
//				if($re){
//					$info['status']=1;
//					$info['msg']='新增分组成功!';
//					//=更新缓存=====
//					$authname = 'wechat_group';
//					S($authname,null);			//删除缓存
//					$data=self::$wx->groups();
//					S($authname,$data,60*60*2);		//2小时缓存一次
//					//=更新缓存=====
//				}else{
//					$info['status']=0;
//					$info['msg']='新增分组失败!';
//				}
//			}
//			$this->ajaxReturn($info);
//		}
//		if($id){
//			$data=$this->get_group();
//			$groupone=array();
//			foreach ($data as $k => $v){
//				if($v['id']==$id){
//					$groupone=$v;
//				}
//			}
//		}
//		$this->assign('cache',$groupone);
//		$this->display();
//	}
//	public function group_del(){
//		$id=I('id');
//		if($id){
//			$re=self::$wx->group_del($id);
//			if($re){
//				$info['status']=1;
//				$info['msg']='删除成功!';
//				//=更新缓存=====
//				$authname = 'wechat_group';
//				S($authname,null);			//删除缓存
//				$data=self::$wx->groups();
//				S($authname,$data,60*60*2);	//2小时缓存一次
//				//=更新缓存=====
//			}else{
//				$info['status']=0;
//				$info['msg']='删除失败!';
//			}
//		}else{
//			$info['status']=0;
//			$info['msg']='分组id未找到!';
//		}
//		$this->ajaxReturn($info);
//	}
//	
	//更新微信会员
	public function upuserlist(){
		ini_set('max_execution_time', '0');
		header("Content-Type: text/html;charset=utf-8");
		$m=M('weixin_auntie_user');
		exit;
//		$options['appid']=C('BB_APPID_client');
//		$options['secret']=C('BB_SECRET_client');

//		$options['appid']=C('BB_APPID_business');
//		$options['secret']=C('BB_SECRET_business');
		
//		$options['appid']=C('BB_APPID_auntie');
//		$options['secret']=C('BB_SECRET_auntie');
		p($options);
		self::$wx=new \bb\Wechat($options);		//new 微信类
		
		$list=self::$wx->users();
		
		foreach ($list['openid'] as $k => $v){
			$user=$m->where(array('openid'=>$v))->find();
			if($user){
				$userdata=self::$wx->user($v);
				$userdata['id']=$user['id'];
				$m->save($userdata);
				echo '更新微信会员  -》'.$user['nickname']."<br/>";
			}else{
				$userdata=self::$wx->user($v);
				$m->add($userdata);
				echo '添加微信会员  -》'.$v."<br/>";
			}
			usleep(1000);
			ob_flush(); //强制将缓存区的内容输出
    		flush(); //强制将缓冲区的内容发送给客户
		}
	}
	
//===========================================================================
//	//客服管理
//	public function kefu(){
//		$bread=array(
//			'0'=>array(
//				'name'=>'微信',
//				'url'=>U('Admin/Wx/index')
//			),
//			'1'=>array(
//				'name'=>'客服管理',
//				'url'=>U('Admin/Wx/group')
//			)
//		);
//		$this->assign('breadhtml',$this->getBread($bread));
//		$m=M('weixin_kefu');
//		$p=$_GET['p']?$_GET['p']:1;
//		$psize=C('BB_PSIZE')?C('BB_PSIZE'):20;
//		$cache=$m->where($mp)->page($p,$psize)->order('addtime desc')->select();
//		$count=$m->where($mp)->count();
//		$this->getPage($count, $psize, 'bb-loader', '公众号会员','bb-search');
//		$data=array();
//		foreach ($cache as $k => $v){
//			$data[$k]=M('weixin_user')->where(array('openid'=>$v['openid']))->find();
//			$data[$k]['id']=$v['id'];
//		}
//		$this->assign('cache',$data);
//		$this->display();
//	}
//	
//	//客服设置
//	public function kefu_set(){
//		if(IS_POST){
//			$p=I('post.');
//			$map['id']  = array('in',rtrim($p['ids'],","));
//			$openid=M('weixin_user')->where($map)->Field('openid')->select();
//			foreach ($openid as $k => $v) {
//				$kefu=M('weixin_kefu')->where(array('openid'=>$v['openid']))->find();
//				if(!$kefu){
//					$kf['openid']=$v['openid'];
//					$kf['addtime']=time();
//					M('weixin_kefu')->add($kf);
//					$info['status']=1;
//					$info['msg']='添加成功!';
//				}else{
//					$info['status']=0;
//					$info['msg']='该客服已存在!';
//				}
//			}
//		}else{
//			$info['status']=0;
//			$info['msg']='请使用post访问!';
//		}
//		$this->ajaxReturn($info);
//	}
//	
//	//删除客服
//	public function kefu_del(){
//		if(IS_POST){
//			$p=I('post.');
//			$id=$p['id'];
//			$m=M('weixin_kefu');
//			$re=$m->delete($id);
//			if($re){
//				$info['status']=1;
//				$info['msg']='删除成功！';
//			}else{
//				$info['status']=0;
//				$info['msg']='删除失败！';
//			}
//		}else{
//			$info['status']=0;
//			$info['msg']='请使用post访问!';
//		}
//		$this->ajaxreturn($info);
//	}
//	
//====================================
	public function sctuison(){
		//设置面包导航，主加载器请配置
		$bread=array(
			'0'=>array(
				'name'=>'微信',
				'url'=>U('Admin/Wx/index')
			),
			'1'=>array(
				'name'=>'首次关注回复',
				'url'=>U('Admin/Wx/sctuison')
			)
		);
		$this->assign('breadhtml',$this->getBread($bread));
		//========================================
		$m=M('weixin_keyword');
		$p=$_GET['p']?$_GET['p']:1;
		$psize=C('BB_PSIZE')?C('BB_PSIZE'):20;
		
		$map['wx_id']=1;
		$map['keyword']='首次关注';
		$keyword1=$m->where($map)->page($p,$psize)->select();
		$keywordcount1=$m->where($map)->count();
		$this->getPage($keywordcount1, $psize, 'bb-loader', '关键词回复','bb-search','','page1');
		$this->assign('keyword1',$keyword1);
		$map['wx_id']=2;
		$map['keyword']='首次关注';
		$keyword2=$m->where($map)->page($p,$psize)->select();
		$keywordcount2=$m->where($map)->count();
		$this->getPage($keywordcount2, $psize, 'bb-loader', '关键词回复','bb-search','','page2');
		$this->assign('keyword2',$keyword2);
		$map['wx_id']=3;
		$map['keyword']='首次关注';
		$keyword3=$m->where($map)->page($p,$psize)->select();
		$keywordcount3=$m->where($map)->count();
		$this->getPage($keywordcount3, $psize, 'bb-loader', '关键词回复','bb-search','','page3');
		$this->assign('keyword3',$keyword3);
		
		$this->display();
	}
	
		//关键词微信回复
	public function sctuison_set(){
		//设置面包导航，主加载器请配置
		$bread=array(
			'0'=>array(
				'name'=>'微信',
				'url'=>U('Admin/Wx/index')
			),
			'1'=>array(
				'name'=>'首次关注回复',
				'url'=>U('Admin/Wx/sctuison')
			),
			'2'=>array(
				'name'=>'设置首次关注回复',
				'url'=>U('Admin/Wx/sctuison_set')
			)
		);
		$this->assign('breadhtml',$this->getBread($bread));
		$m=M('weixin_keyword');
		$news=M('weixin_news');
		$id=I('id');
		$wx_id=I('wx_id');
		if(!$wx_id){
			echo '出错了！';
			echo '<a id="alink" href="'.U('Wx/keyword').'" data-loader="bb-loader" data-loadername=""></a>'; 
			echo "<script>initLoader('bb-loader'); setTimeout(function(){ document.getElementById('alink').click(); },1000); </script>";
			exit;
		}
		if(IS_POST){
			$p=I('post.');
			if($id){
				if($p['type']=='text'){
					$add['id']=$p['id'];
					$add['wx_id']=$p['wx_id'];
					$add['keyword']=$p['keyword'];
					$add['type']=$p['type'];
					$add['content']=$p['content'];
					$re=$m->save($add);
					if($re){
						$info['status']=1;
						$info['msg']='修改成功！';
					}else{
						$info['status']=0;
						$info['msg']='修改失败！';
					}
				}else{
					$add['id']=$p['id'];
					$add['wx_id']=$p['wx_id'];
					$add['keyword']=$p['keyword'];
					$add['type']=$p['type'];
					$add['content']=time();
					$re=$m->save($add);
					if($re){
						$datas=array();
						foreach ($p['data'] as $key => $value){
							foreach ($value as $k => $v) {
								$datas[$k][$key]=$v;
							}
						}
						foreach ($datas as $k => $v) {
							$news->save($v);
						}
						$add['content']='';
						$m->save($add);
						$info['status']=1;
						$info['msg']='修改成功！';
					}else{
						$info['status']=0;
						$info['msg']='修改失败！';
					}
				}
			}else{
				$cache=$m->where(array("keyword"=>$p['keyword'],'wx_id'=>$p['wx_id']))->find();
				if($cache){
					$info['status']=0;
					$info['msg']='触发关键词已存在！请更换！';
					$this->ajaxreturn($info);
				}
				if($p['type']=='text'){
					$add['wx_id']=$p['wx_id'];
					$add['keyword']=$p['keyword'];
					$add['type']=$p['type'];
					$add['content']=$p['content'];
					$re=$m->add($add);
					if($re){
						$info['status']=1;
						$info['msg']='添加成功！';
					}else{
						$info['status']=0;
						$info['msg']='添加失败！';
					}
				}else{
					$add['wx_id']=$p['wx_id'];
					$add['keyword']=$p['keyword'];
					$add['type']=$p['type'];
					$add['content']=$p['content'];
					$re=$m->add($add);
					if($re){
						$datas=array();
						foreach ($p['data'] as $key => $value){
							foreach ($value as $k => $v) {
								$datas[$k]['kid']=$re;
								$datas[$k][$key]=$v;
							}
						}
						foreach ($datas as $k => $v) {
							$news->add($v);
						}
						$info['status']=1;
						$info['msg']='添加成功！';
					}else{
						$info['status']=0;
						$info['msg']='添加失败！';
					}
				}
			}
			$this->ajaxreturn($info);
		}
		if($id){
			$cache=$m->where('id='.$id)->find();
			$this->assign('cache',$cache);
			$tmp_data=$news->where('kid='.$id)->select();
			$this->assign('tmp_data',$tmp_data);	
		}
		$this->assign('wx_id',$wx_id);
		$this->display();
	}
	//=设置业务员================
	public function agent_set(){
		if(IS_POST){
			if(I('ids')){
				$p=I('post.');
				$u_p=explode(",",$p['ids']);
				$map['id']=$u_p[0];
				$u=M('weixin_client_user')->where($map)->Field('openid')->find();
				if($u){
					$this->assign('openid',$u['openid']);
					$this->display();
				}
			}else{
				$p=I('post.');
				$u=M('bb_agent')->where(array('openid'=>$p['openid']))->find();
				if(!$u){
					$p['addtime']=date("Y-m-d H:i:s",time()); 
					$re=M('bb_agent')->add($p);
					if($re){
						$info['status']=1;
						$info['msg']='添加成功！';
					}else{
						$info['status']=0;
						$info['msg']='添加失败！';
					}
				}else{
					$info['status']=0;
					$info['msg']='该微信会员已是业务员！';
				}
				$this->ajaxReturn($info);
			}
		}
	}
	//============
	public function aa(){
		echo 123;
	}
}